package com.lmonkey.servlet.home;

import com.lmonkey.servic.CARTDao;
import com.lmonkey.servic.CATAGORYDao;
import com.lmonkey.servic.PRODUCTDao;
import com.lmonkey.shop.CATEGORY;
import com.lmonkey.shop.PRODUCT;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/shangc/selectproductlist")
public class SelectProductList extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //菜单
        ArrayList<CATEGORY> flist = CATAGORYDao.selectcat("father");
        request.setAttribute("flist",flist);
        ArrayList<CATEGORY> clist = CATAGORYDao.selectcat("child");
        request.setAttribute("clist",clist);

        String fid = request.getParameter("fid");
        String cid = request.getParameter("cid");
        int id = 0;
        ArrayList<PRODUCT> list = null;
        if (fid != null){
            id = Integer.parseInt(fid);
            list = PRODUCTDao.selectByfid(id);
            request.setAttribute("fid",fid);
        }
        if (cid != null){
            id = Integer.parseInt(cid);
            list = PRODUCTDao.selectBycid(id);
            request.setAttribute("cid",cid);
        }

        //获取id
        String pid = request.getParameter("id");
        PRODUCT plist = null;
        if (pid != null){
            System.out.println(pid);
            int id1 = Integer.parseInt(pid);
            plist = PRODUCTDao.selectByid(id1);
            request.setAttribute("pp",plist);
        }


        //判断用户是否登入
        HttpSession session = request.getSession();
        String isLogin = (String)session.getAttribute("isLogin");
        USER user = (USER)session.getAttribute("name");
        if (user != null && isLogin.equals("1")){
            String uid = user.getuser_id();
            int count = CARTDao.selectByid(uid);
            request.setAttribute("num",count);
        }

        request.setAttribute("title",CATAGORYDao.selectByid(id).getCate_name());
        request.setAttribute("list",list);

        request.getRequestDispatcher("productlist.jsp").forward(request,response);
    }
}
